﻿@page "/articles"
@page "/articles/pages"
@page "/articles/pages/{pageNumber:int}"
@inherits BaseArticlePage
@inject AcBlog.Services.Filters.PostArticleFilter Article

<PageContainer Title="Articles" PageTitle="@Title">
    <Breadcrumb>
        <Breadcrumb>
            <BreadcrumbItem>
                <a href="">
                    <MdiIcon Type="@IconNames.Home" />
                    <span>Home</span>
                </a>
            </BreadcrumbItem>
            <BreadcrumbItem>
                <a href="@UrlGenerator.Posts()">
                    <MdiIcon Type="@IconNames.Posts" />
                    <span>Posts</span>
                </a>
            </BreadcrumbItem>
            <BreadcrumbItem>
                <span>
                    <MdiIcon Type="@IconNames.Articles" />
                    <span>Articles</span>
                </span>
            </BreadcrumbItem>
        </Breadcrumb>
    </Breadcrumb>
    <Extra>
        <AuthorizeView>
            <Authorized>
                <ButtonLink Href="@($"posts/create/article")">
                    <MdiIcon Type="@IconNames.Create"></MdiIcon>
                    <span>Create</span>
                </ButtonLink>
            </Authorized>
        </AuthorizeView>
    </Extra>
    <ChildContent>
        <Loader OnLoad="@OnLoad" LoadKey="@($"{PageNumber}")" @ref="loader">
            <PagingList Data="@Data" Column="1" PageNavigationUrl="@UrlGenerator.Articles()">
                <ItemTemplate>
                    <PostListItem Id="@context"></PostListItem>
                </ItemTemplate>
            </PagingList>
        </Loader>
    </ChildContent>
</PageContainer>

@code {
    const int PageSize = 10;

    PagingData<string> Data { get; set; }

    [Parameter]
    public int? PageNumber { get; set; } = null;

    private Loader loader;

    private async Task OnLoad()
    {
        Title = "Loading...";
        Data = await Article.Filter(new Data.Models.Actions.Pagination
        {
            CurrentPage = PageNumber ?? 0,
            PageSize = PageSize
        }, QueryTimeOrder.CreationTimeDescending);
        Title = $"Page {Data.CurrentPage.CurrentPage + 1}";
    }
}
